<?php
/**
 * admin_node 数据表模型
 * @author  haiwera <zhanhh@pvc123.com>
 */

namespace models;

use herosphp\cache\CacheFactory;
use herosphp\core\WebApplication;

class AdminNodeModel extends C_Model {

	private static $__cacheCtrl ;
	private static $__hasModified = false;
	private $__nodeArray = array();
    public function __construct() {

        //创建model对象并初始化数据表名称
        parent::__construct('admin_node');

        //设置表数据表主键，默认为id
        $this->setPrimaryKey('id');
		$this->autoPrimary = false;
		$this->appFilter = false;

        //客户机运行不需要缓存
		if(self::$__cacheCtrl == null && !defined('RUN_CLI'))
		{
			$config = WebApplication::getInstance()->getConfigs();
			$cachetype = $config['cache_type'];

			self::$__cacheCtrl = CacheFactory::create($cachetype);
		}
    }

	/*
	*  获取权限数组
	*/
	public function getNodeArray()
	{
		if(empty($this->__nodeArray))
		{
			$this->flushNodeArray();
		}
		return $this->__nodeArray;
	}
	/*
	* 刷新权限缓存
	*/
	public function flushNodeArray()
	{
		$cache = self::$__cacheCtrl->baseKey('system')->ftype('node')->factor();
		$result = $cache->get();
		if(!$result || self::$__hasModified)
		{
			$list = $this->getItems(null,null,'node_key DESC',array(1,10000));
			$result = $list;
			$cache->set(null,$result);
		}
		$this->__nodeArray = $result;
		self::$__hasModified = false;
	}
	/*
	* @override : 插入权限
	*/
	public function add($data)
	{
		$rs = parent::add($data);
		if($rs)
		{
			self::$__hasModified = true;
			$this->flushNodeArray();
		}
		return $rs;
	}
	/*
	* @override : 更新权限
	*/
	public function update($data,$id)
	{
		$rs = parent::update($data,$id);
		if($rs)
		{
			self::$__hasModified = true;
			$this->flushNodeArray();

		}
		return $rs;
	}

	/*
	* @override 
	 */
	public function updates($data,$conditions){
		$rs = parent::updates($data,$conditions);
		if($rs){
			self::$__hasModified = true;
			$this->flushNodeArray();
		}
		return $rs;
	}

	/*
	* @override : 删除权限
	*/

	public function delete($id)
	{
		$rs = parent::delete($id);
		if($rs)
		{
			self::$__hasModified = true;
			$this->flushNodeArray();
		}
		return $rs;
	}
} 
